package lzj.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

  public static void main(String[] args) {
    try {
//      readLine("C:\\Users\\luzhijie\\Desktop\\wenwuju.sql");
      Write("C:\\Users\\luzhijie\\Desktop\\wenwuju.sql", "你好呀");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 文件按照行进行读取并返会list
   */
  public static List<String> readLine(String filepath) throws IOException {
    List<String> list = new ArrayList<>();
    //打开待读取的文件
    BufferedReader br = new BufferedReader(new FileReader(filepath));
    String line = null;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
      list.add(line);
//      position++;
    }
    br.close();//关闭文件
    return list;
  }

  /**
   * 按照字节流进行读取文件内容
   */
  public static String readFileByByte(String filepath) throws IOException {

    StringBuffer s = new StringBuffer();
    File f = new File(filepath);
    InputStream in = new FileInputStream(f);
    int tempByte;
    while ((tempByte = in.read()) != -1) {
      s.append(tempByte);
    }
    in.close();
    return s.toString();
  }

  /**
   * 一次性插入数据
   */
  public static void Write(String filepath, String content) throws IOException {
    OutputStream os = new FileOutputStream(filepath);
    PrintWriter pw = new PrintWriter(os);
    pw.println(content);//每输入一个数据，自动换行，便于我们每一行每一行地进行读取
    pw.close();
    os.close();
  }

  /**
   * 多行插入
   */
  public static void Write(String filepath, List<String> listcontent) throws IOException {
    OutputStream os = new FileOutputStream(filepath);
    PrintWriter pw = new PrintWriter(os);

    for (String content : listcontent) {
      pw.println(content);//每输入一个数据，自动换行，便于我们每一行每一行地进行读取
    }
    pw.close();
    os.close();
  }

  /**
   * pdf 转图片
   *
   * @param file 文件对象
   * @param savepath 相对项目的保存路径
   * @param pathname pdf文件名不带后缀
   */

 /* private List<String> pdfToJpg(File file, String savepath, String pathname) {
    List<String> list = new ArrayList<>();
    try {
      String pathA = "/usr/local/tomcat/webapps/";
      PDDocument doc = PDDocument.load(file);

      PDFRenderer renderer = new PDFRenderer(doc);
      int pageCount = doc.getNumberOfPages();
      String returnpath = "";
      for (int i = 0; i < pageCount; i++) {
        BufferedImage image = renderer.renderImageWithDPI(i, 96);
        BufferedImage srcImage = resize(image, 1080, 1920);
        String jpgpath = pathA + savepath + pathname + "/";

        File jpgFilepath = new File(jpgpath);
        if (!jpgFilepath.exists()) {
          jpgFilepath.mkdirs();//自动创建
        }
        String jpgName = i + ".JPG";
        ImageIO.write(srcImage, "JPG", new File(jpgpath + jpgName));
        list.add(savepath + pathname + "/" + jpgName);

      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }*/

}
